package cn.sunny360;

import com.microsoft.playwright.*;

public class Main {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false));
            //Page page = browser.newPage();
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate("https://passport.jd.com/uc/login");
            context.waitForCondition(()->page.url().equals("https://www.jd.com/"));
            page.locator("#key").fill("手机");
            page.locator("#key").press("Enter");
            /*page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("1.png"))
                    // 这个网页，如果没有这个参数只截取可见部分
                    //.setFullPage(true)
            );*/
            /*Locator searchText = page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("搜索"));
            searchText.fill("手机");
            page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("搜索")).click();*/
//            page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("我的购物车")).click();
            // 捕获弹窗
            /*page.onPopup(p->{
                System.out.println("p--" + p.url());
                System.out.println("p--" + p.title());
            });
            // 页面跳转
            page.onLoad(p2->{
                System.out.println("p2--" + p2.url());
                System.out.println("p2--" + p2.title());
            });*/
            System.out.println(page.url());
            System.out.println(page.title());
            //System.out.println(page.content());
            /*page.getByRole(AriaRole.LINK).all()
                            .forEach(b->{
                                System.out.println("link-" + b.textContent());
                            });*/
            page.pause();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}